#include "homewidget.h"
#include "ui_homewidget.h"

HomeWidget::HomeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomeWidget)
{
    ui->setupUi(this);

    QRect d = QApplication::desktop()->availableGeometry();

    this->move((d.width() - this->width()), 0);
    this->resize(this->width(), d.height() - 80);

    this->stalkModel = StalkModel::getInstance();

    connect(this->stalkModel, SIGNAL(contactsFullUpdate(QVariantMap)), this, SLOT(onContactsFullUpdate(QVariantMap)));
}

HomeWidget::~HomeWidget()
{
    delete ui;
}

void HomeWidget::showEvent(QShowEvent *)
{
    ui->myStatusComboBox->setCurrentIndex(1);

    StalkModel::getInstance()->startVoiceBenchmark();
}

void HomeWidget::on_myStatusComboBox_currentIndexChanged(int index)
{
    this->stalkModel->setStatus(index);
}

void HomeWidget::onContactsFullUpdate(QVariantMap m)
{
    ui->contactListWidget->clear();
    QListWidgetItem *listItem;
    qDebug() << m;
    foreach(const QVariant contactItem, m["friends"].toList())
    {
        listItem = new QListWidgetItem(QIcon(tr(":/res/images/%1.png").arg(contactItem.toMap()["status_id"].toString())), contactItem.toMap()["name"].toString());
        ui->contactListWidget->addItem(listItem);
    }
}
